package com.inspur.system.service.impl;

import java.util.Date;
import java.util.List;

import com.inspur.common.utils.SecurityUtils;
import com.inspur.common.utils.StringUtils;
import com.inspur.common.utils.uuid.UUID;
import com.inspur.system.domain.vo.SysUserSaveProjVo;
import com.inspur.system.dto.PmTaskDTO;
import com.inspur.system.dto.ProNumDTO;
import com.inspur.system.mapper.PmDynMapper;
import com.inspur.system.mapper.SysUserMapper;
import com.inspur.system.utils.PmDynUtils;
import com.inspur.system.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.inspur.system.mapper.PmTaskMapper;
import com.inspur.system.domain.PmTask;
import com.inspur.system.service.IPmTaskService;

/**
 * 任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-08
 */
@Service
public class PmTaskServiceImpl implements IPmTaskService 
{
    @Autowired
    private PmTaskMapper pmTaskMapper;

    @Autowired
    private PmDynMapper pmDynMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询任务
     * 
     * @param cTaskNo 任务主键
     * @return 任务
     */
    @Override
    public PmTaskDTO selectPmTaskByCTaskNo(String cTaskNo)
    {
        return pmTaskMapper.selectPmTaskByCTaskNo(cTaskNo);
    }

    /**
     * 查询任务列表
     * 
     * @param pmTask 任务
     * @return 任务
     */
    @Override
    public List<PmTask> selectPmTaskList(PmTask pmTask)
    {
        return pmTaskMapper.selectPmTaskList(pmTask);
    }

    /**
     * 查询任务列表
     *
     * @param pmTask 任务
     * @return 任务
     */
    @Override
    public List<PmTaskDTO> selectPmTaskDTOList(PmTaskDTO pmTask)
    {
        List<PmTaskDTO> pmTaskDTOList = pmTaskMapper.selectPmTaskDTOList(pmTask);
        for (PmTaskDTO pmTaskDTO : pmTaskDTOList) {
            Double cEstWork = pmTaskDTO.getcEstWork(); // 预估工时
            Double cRemWork = pmTaskDTO.getcRemWork(); // 剩余工时
            String cEstRemWork = cEstWork + "h/" + cRemWork + "h";
            pmTaskDTO.setcEstRemWork(cEstRemWork);
        }
        return pmTaskDTOList;
    }

    /**
     * 新增任务
     * 
     * @param pmTask 任务
     * @return 结果
     */
    @Override
    public int insertPmTask(PmTask pmTask)
    {
        String pmTaskNo = "13" + UUID.generateShortUuid();
        pmTask.setcTaskNo(pmTaskNo);
        Long userId = SecurityUtils.getUserId();
        pmTask.setcCrePer(userId+"");
        pmTask.setcUpdPer(userId+"");
        pmTask.settCreTm(new Date());
        pmTask.settUpdTm(new Date());
        pmDynMapper.insertPmDyn(PmDynUtils.addDynCom("创建了工作项","1",pmTask.getcTaskNo()));
        // 同步用户表
        List<SysUserSaveProjVo> users = UserUtils.updatePmNo(userId,pmTask.getcTaskPer(),pmTask.getcTaskNo(),pmTask.getcTaskHead());
        users.forEach(user -> userMapper.updateUserByProj(user));
        return pmTaskMapper.insertPmTask(pmTask);
    }

    /**
     * 修改任务
     * 
     * @param pmTask 任务
     * @return 结果
     */
    @Override
    public int updatePmTask(PmTask pmTask)
    {
        PmTaskDTO oldTask = pmTaskMapper.selectPmTaskByCTaskNo(pmTask.getcTaskNo());
        if (StringUtils.isNotEmpty(oldTask.getcTaskState())) {
            if (!oldTask.getcTaskState().equals(pmTask.getcTaskState())) {
                String updateState = PmDynUtils.updateState(oldTask.getcTaskState(),pmTask.getcTaskState());
                pmDynMapper.insertPmDyn(PmDynUtils.addDynCom(updateState,"3",pmTask.getcTaskNo()));
            }
        } else {
            if (StringUtils.isNotEmpty(pmTask.getcTaskState())) {
                String updateState = PmDynUtils.updateState("0",pmTask.getcTaskState());
                pmDynMapper.insertPmDyn(PmDynUtils.addDynCom(updateState,"3",pmTask.getcTaskNo()));
            }
        }
        pmDynMapper.insertPmDyn(PmDynUtils.addDynCom("修改了工作项","1",pmTask.getcTaskNo()));
        return pmTaskMapper.updatePmTask(pmTask);
    }

    /**
     * 批量删除任务
     * 
     * @param cTaskNos 需要删除的任务主键
     * @return 结果
     */
    @Override
    public int deletePmTaskByCTaskNos(String[] cTaskNos)
    {
        return pmTaskMapper.deletePmTaskByCTaskNos(cTaskNos);
    }

    /**
     * 删除任务信息
     * 
     * @param cTaskNo 任务主键
     * @return 结果
     */
    @Override
    public int deletePmTaskByCTaskNo(String cTaskNo)
    {
        return pmTaskMapper.deletePmTaskByCTaskNo(cTaskNo);
    }

    @Override
    public ProNumDTO selectPmTaskNum()
    {
        return pmTaskMapper.selectPmTaskNum();
    }
}
